﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

using Joyintech.Common;
using Joyintech.BLL.inpiao365;
using Joyintech.Model.inpiao365;

namespace Joyintech.Web.inpiao365.UserCenter
{
    public partial class UserLogin : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!this.IsPostBack)
            {
                //从Memcached中移出名称为Session的值
                customer_BLL.GetInstance().ClearCurrentCustomer();
            }
        }

        protected void btnLogin_Click(object sender, ImageClickEventArgs e)
        {
            string strError = string.Empty;

            if (this.txtUserName.Text.Trim() == "")
            {
                strError += "用户名不可为空";
            }

            if (this.txtPwd.Text.Trim() == "")
            {
                strError += "密码不可为空";
            }

            if (!strError.Equals(""))
            {
                MessageBox.Show(this, strError);
                return;
            }

            customer model = new customer();

            model = customer_BLL.GetInstance().GetModel(this.txtUserName.Text.Trim());

            if (model != null && model.CustomerPass == Utils.MD5(this.txtPwd.Text.Trim()))
            {
                //更新最后登录的时间
                customer newModel = model;
                newModel.CustomerLastLoginTime = DateTime.Now;
                customer_BLL.GetInstance().Update(newModel);

                //向Cookies中存储SessionId
                customer_BLL.GetInstance().StoreSessionId(HttpContext.Current.Session.SessionID);

                //向Memcached中存储SessionId对应的当前登录会员实体
                customer_BLL.GetInstance().StoreCurrentCustomer(HttpContext.Current.Session.SessionID, model);

                //根据用户居住地获取用户的城市Id
                List<movieCity> listMovieCity = new movieCity_BLL().GetModelList("CityName='%" + model.CustomerResidence + "%'");

                if (listMovieCity != null && listMovieCity.Count > 0)
                {
                    //将用户所属城市id写入Cookies，名称为“ztyingonde”                
                    Utils.WriteCookie("ztyingonde", listMovieCity[0].CityId.ToString());
                }

                if (DNTRequest.GetQueryString("cmsurl") != "")
                {
                    Response.Redirect(DNTRequest.GetQueryString("cmsurl"));
                }
                else
                {
                    Response.Redirect("MyTicket_w.aspx");
                }
            }
            else
            {
                MessageBox.Show(this, "用户名或密码错误，请重新输入！");
            }
        }
    }
}